package com.kevin.state.machine.transaction;

import com.kevin.state.machine.enums.Action;
import com.kevin.state.machine.enums.State;
import com.kevin.state.machine.event.Event;
import lombok.Getter;
import lombok.ToString;
import lombok.experimental.Accessors;

/**
 * 状态事务转换
 */
@Getter
@ToString
@Accessors(chain = true)
public class Transaction<O> {

    // 当前状态
    private State currentState;
    // 对应行为
    private Action action;
    // 下个状态
    private State nextState;
    // 对应事件
    private Event<O> event;

    public Transaction() {

    }

    // 前置状态
    public Transaction<O> source(State state) {
        currentState = state;
        return this;
    }

    // 触发条件
    public Transaction<O> when(Action action) {
        this.action = action;
        return this;
    }

    // 目标状态
    public Transaction<O> target(State state) {
        nextState = state;
        return this;
    }

    // 事件处理
    public void handle(Event<O> event) {
        this.event = event;
    }

}
